package org.appfuse.webapp.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.appfuse.Constants;
import org.appfuse.model.BasicInfo;
import org.appfuse.model.DutyExperience;
import org.appfuse.model.User;
import org.appfuse.service.BasicInfoManager;
import org.appfuse.service.DutyExperienceManager;
import org.appfuse.webapp.form.DutyExperienceForm;

/**
 * Action class to handle CRUD on a Menu object
 * 
 * @struts.action name="criteriaForm" path="/criterias" scope="request"
 *                validate="false" parameter="method" input="mainMenu"
 * @struts.action name="criteriaForm" path="/editMenu" scope="request"
 *                validate="false" parameter="method" input="list"
 * @struts.action name="criteriaForm" path="/saveMenu" scope="request"
 *                validate="true" parameter="method" input="edit"
 * @struts.action-set-property property="cancellable" value="true"
 * @struts.action-forward name="edit" path="/WEB-INF/pages/criteriaForm.jsp"
 * @struts.action-forward name="list" path="/WEB-INF/pages/criteriaList.jsp"
 * @struts.action-forward name="search" path="/criterias.html" redirect="true"
 */
public final class DutyExperienceAction extends BaseAction {

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("list");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'delete' method");
		}
		DutyExperienceForm form = (DutyExperienceForm) actionForm;

		User currentUser = getCurrentUser(request);

		DutyExperienceManager dutyExperienceMgr = (DutyExperienceManager) getBean(Constants.DUTYEXPERIENCE_MANAGER);
		Integer selectedId = form.getSelectedId();
		String a00 = currentUser.getRecordId();
		BasicInfoManager basicInfoMgr = (BasicInfoManager) getBean(Constants.BASICINFO_MANAGER);
		BasicInfo basicInfo = (BasicInfo) basicInfoMgr.findById(a00);

		DutyExperience dutyExp = (DutyExperience) convert(form);
		dutyExp.setId(selectedId);

		try {
			DutyExperienceManager mgr = (DutyExperienceManager) getBean(Constants.DUTYEXPERIENCE_MANAGER);
			dutyExp.setBasicInfo(basicInfo);
			mgr.delete(dutyExp);
			HttpSession session = request.getSession();
			List dutyExpList = (List) session
					.getAttribute(Constants.DUTYEXPERIENCE_LIST);
			dutyExpList.remove(dutyExp);

		} catch (Exception e) {

		}

		return mapping.findForward("list");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'edit' method");
		}

		DutyExperienceForm form = (DutyExperienceForm) actionForm;

		User currentUser = getCurrentUser(request);

		DutyExperienceManager dutyExperienceMgr = (DutyExperienceManager) getBean(Constants.DUTYEXPERIENCE_MANAGER);
		Integer selectedId = form.getSelectedId();
		String a00 = currentUser.getRecordId();
		DutyExperience dutyExp = dutyExperienceMgr.findById(a00, selectedId);

		if (dutyExp != null) {
			form = (DutyExperienceForm) convert(dutyExp);
			updateFormBean(mapping, request, form);
		}

		return mapping.findForward("edit");
	}

	public ActionForward save(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		ActionMessages errors = actionForm.validate(mapping, request);

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			return mapping.findForward("edit");
		}

		DutyExperienceManager mgr = (DutyExperienceManager) getBean("dutyExperienceManager");
		User user = getCurrentUser(request);
		String a00 = user.getRecordId();

		// Extract attributes and parameters we will need
		ActionMessages messages = new ActionMessages();
		DutyExperienceForm form = (DutyExperienceForm) actionForm;
		DutyExperience theObject = (DutyExperience) convert(form);

		BasicInfoManager basicInfoMgr = (BasicInfoManager) getBean("basicInfoManager");
		BasicInfo basicInfo = (BasicInfo) basicInfoMgr.findById(a00);

		HttpSession session = request.getSession();
		List dutyExpList = (List) session
		.getAttribute(Constants.DUTYEXPERIENCE_LIST);

		try {
			// if id is not null then udpate the record else add it.
			if (form.getId() != null) {
				theObject.setBasicInfo(basicInfo);
				mgr.update(theObject);

				int index = dutyExpList.indexOf(theObject);
				dutyExpList.set(index, theObject);
			} else {
				theObject.setId(mgr.newId(a00));
				basicInfo.getDutyExperiences().add(theObject);
				theObject.setBasicInfo(basicInfo);
				// mgr.save(dutyExp);
				basicInfoMgr.update(basicInfo);
				form.setId(theObject.getId());

				dutyExpList.add(theObject);

			}
			
			theObject.setRzjgmc((String) Constants.G099_MAP.get(theObject.getRzjgmc()));
			theObject.setRzjgtjbs((String) Constants.BJ008_MAP.get(theObject.getRzjgtjbs()));
			theObject.setZwmc((String) Constants.GB12403_MAP.get(theObject.getZwmc()));
			theObject.setZj((String) Constants.ZB09_MAP.get(theObject.getZj()));
			theObject.setRzzt((String) Constants.ZB14_MAP.get(theObject.getRzzt()));
			theObject.setZwlb((String) Constants.ZB42_MAP.get(theObject.getZwlb()));
			theObject.setLdzw((String) Constants.BJ001_MAP.get(theObject.getLdzw()));
			theObject.setRzjgxzlb((String) Constants.ZB04_MAP.get(theObject.getRzjgxzlb()));
			theObject.setTmrzjgmc((String) Constants.G099_MAP.get(theObject.getTmrzjgmc()));
			theObject.setPzrzjgmc((String) Constants.G099_MAP.get(theObject.getPzrzjgmc()));
			theObject.setRzyylb((String) Constants.ZB12_MAP.get(theObject.getRzyylb()));
			theObject.setRzbdlb((String) Constants.ZB13_MAP.get(theObject.getRzbdlb()));
			theObject.setRzfs((String) Constants.ZB11_MAP.get(theObject.getRzfs()));
			theObject.setTmmzjgmc((String) Constants.G099_MAP.get(theObject.getTmmzjgmc()));
			theObject.setPzmzjgmc((String) Constants.G099_MAP.get(theObject.getPzmzjgmc()));
			theObject.setMzyylb((String) Constants.ZB16_MAP.get(theObject.getMzyylb()));
			theObject.setMzbdlb((String) Constants.ZB13_MAP.get(theObject.getMzbdlb()));
			theObject.setMzfs((String) Constants.ZB15_MAP.get(theObject.getMzfs()));
			theObject.setJllb((String) Constants.BJ90_MAP.get(theObject.getJllb()));
			theObject.setCszy((String) Constants.GB_MAP.get(theObject.getCszy()));
			session
			.setAttribute(Constants.DUTYEXPERIENCE_LIST,
					dutyExpList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// add success messages
		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				"dutyExperience.saved"));
		// save messages in session to survive a redirect
		saveMessages(request.getSession(), messages);

		return mapping.findForward("list");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		return mapping.findForward("edit");
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'list' method");
		}

		User user = getCurrentUser(request);
		HttpSession session = request.getSession();

		String a00 = user.getRecordId();
		if (!StringUtils.isEmpty(a00)) {
			DutyExperienceManager mgr = (DutyExperienceManager) getBean(Constants.DUTYEXPERIENCE_MANAGER);
			List list = mgr.findByA00(a00);
			if (list != null){
				
				Iterator it = list.iterator();
				while (it.hasNext()){
					DutyExperience theObject = (DutyExperience) it.next();
					theObject.setRzjgmc((String) Constants.G099_MAP.get(theObject.getRzjgmc()));
					theObject.setRzjgtjbs((String) Constants.BJ008_MAP.get(theObject.getRzjgtjbs()));
					theObject.setZwmc((String) Constants.GB12403_MAP.get(theObject.getZwmc()));
					theObject.setZj((String) Constants.ZB09_MAP.get(theObject.getZj()));
					theObject.setRzzt((String) Constants.ZB14_MAP.get(theObject.getRzzt()));
					theObject.setZwlb((String) Constants.ZB42_MAP.get(theObject.getZwlb()));
					theObject.setLdzw((String) Constants.BJ001_MAP.get(theObject.getLdzw()));
					theObject.setRzjgxzlb((String) Constants.ZB04_MAP.get(theObject.getRzjgxzlb()));
					theObject.setTmrzjgmc((String) Constants.G099_MAP.get(theObject.getTmrzjgmc()));
					theObject.setPzrzjgmc((String) Constants.G099_MAP.get(theObject.getPzrzjgmc()));
					theObject.setRzyylb((String) Constants.ZB12_MAP.get(theObject.getRzyylb()));
					theObject.setRzbdlb((String) Constants.ZB13_MAP.get(theObject.getRzbdlb()));
					theObject.setRzfs((String) Constants.ZB11_MAP.get(theObject.getRzfs()));
					theObject.setTmmzjgmc((String) Constants.G099_MAP.get(theObject.getTmmzjgmc()));
					theObject.setPzmzjgmc((String) Constants.G099_MAP.get(theObject.getPzmzjgmc()));
					theObject.setMzyylb((String) Constants.ZB16_MAP.get(theObject.getMzyylb()));
					theObject.setMzbdlb((String) Constants.ZB13_MAP.get(theObject.getMzbdlb()));
					theObject.setMzfs((String) Constants.ZB15_MAP.get(theObject.getMzfs()));
					theObject.setJllb((String) Constants.BJ90_MAP.get(theObject.getJllb()));
					theObject.setCszy((String) Constants.GB_MAP.get(theObject.getCszy()));
				}
			}
			
			session.setAttribute(Constants.DUTYEXPERIENCE_LIST, list);
		}

		return mapping.findForward("list");
	}
}
